# Simple AJAX chess website - All data models for the application
#
# Copyright (C) 2008  Hong Anh Khoa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from google.appengine.ext import db

class Room(db.Model):
  # A room for a game. Contains:
  #   - Creator (will have some special permission)
  #   - Room name
  #   - Create date
  #   - Last access (for tracking current game room)
  #   - Password
  #   - Active (currently active or not)
  
  creator = db.UserProperty(required = True)
  name = db.StringProperty()
  created = db.DateTimeProperty(auto_now_add = True)
  accessed = db.DateTimeProperty(auto_now = True)
  password = db.StringProperty()
  active = db.BooleanProperty()
  status = db.StringProperty()
  users = db.IntegerProperty()
  

class Chat(db.Model):
  # A chat text. Contains:
  #  - Author
  #  - Room ID
  #  - Sent time
  #  - Content
  
  author = db.UserProperty()
  room = db.ReferenceProperty(Room)
  sent = db.DateTimeProperty(auto_now_add = True)
  content = db.StringProperty(required = True)


class Player(db.Model):
  # Data associated with a user. Contains
  #  - Google account
  #  - Last access
  
  user = db.UserProperty(required = True)
  access = db.DateTimeProperty(auto_now = True)
  

class Game(db.Model):
  # Data of a game. Contains:
  #  - Room
  #  - White player
  #  - Black player
  #  - Started time
  #  - GameObject
  #  - Result
  
  room = db.ReferenceProperty(reference_class = Room, required = True)
  white = db.UserProperty(required = True)
  black = db.UserProperty(required = True)
  started = db.DateTimeProperty(auto_now_add = True)
  gameobject = db.BlobProperty()
  result = db.StringProperty()
  playing = db.BooleanProperty()
  

class RoomPlayer(db.Model):
  # List of players in a room. Contains:
  #  - Room
  #  - User
  #  - Access: Last access of a user to a room. Used to determined if user is currently in the room
  
  room = db.ReferenceProperty(reference_class = Room, required = True)
  user = db.UserProperty(required = True)
  access = db.DateTimeProperty(auto_now = True)
  color = db.StringProperty(choices = set(['black', 'white']))
  status = db.StringProperty(choices = set(['playing', 'finished']))